package com.image;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import org.xhtmlrenderer.simple.ImageRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Properties;

public class SCBillReport {

    public static void main(String[] args) throws IOException, TemplateException {
        // 创建一个 Configuration 实例
        Configuration cfg = new Configuration(new Version("2.3.29"));
        cfg.setDefaultEncoding("UTF-8");
        // 设置模板文件所在的目录
        cfg.setClassForTemplateLoading(SCBillReport.class, "/templates");
        cfg.setSettings(new Properties(System.getProperties()));

        // 填充ftl 并 写出html文件
        Template template = cfg.getTemplate("business_data.ftl");
        HashMap<String, Object> dataModel = new HashMap<>();
        dataModel.put("MM_DD", "7月1日");
        //总规模（万元）
        dataModel.put("overall_scope", Arrays.asList("300", "400", "80", "100", "20", "70", "300000000"));
        //昨日总数
        dataModel.put("yesterdays_scope", Arrays.asList("300", "400", "80", "100", "20", "70", "30"));
        //沂链通
        dataModel.put("yiliantong_scope", Arrays.asList("300", "400", "80", "100", "20", "70", "30"));
        //商票板
        dataModel.put("spb_scope", Arrays.asList("300", "400", "80", "100", "20", "70", "30"));
        //承信账户
        dataModel.put("cx_account_scope", Arrays.asList("300", "400", "80", "100", "20", "70", "30"));
        //滨链通
        dataModel.put("binliantong_scope", Arrays.asList("300", "400", "80", "100", "20", "70", "30"));
        //其他
        dataModel.put("others", Arrays.asList("300", "400", "80", "100", "20", "70", "30"));
        //业务一部
        dataModel.put("first_department", Arrays.asList("300", "400", "80", "100", "20", "70", "30"));
        //业务三部
        dataModel.put("third_department", Arrays.asList("300", "400", "80", "100", "20", "70", "30"));
        //账户业务部
        dataModel.put("account_department", Arrays.asList("300", "400", "80", "100", "20", "70", "30"));

        //承信账户-总规模
        dataModel.put("account_overall_scope", Arrays.asList("300", "400", "80", "100", "20", "70", "30"));
        //承信账户-昨日新增规模
        dataModel.put("account_yesterdays_scope", Arrays.asList("300", "400", "80", "100", "20", "70", "30"));


        FileWriter htmlWriter = new FileWriter("temp-1.html", StandardCharsets.UTF_8);
        template.process(dataModel, htmlWriter);

        BufferedImage image = ImageRenderer.renderToImage("temp-1.html", "9988.png", 1240);
        File file = new File("9988.png");
        ImageIO.write(image, "PNG", file);
    }
}
